<template>
  <el-select :value="defaultValue" :clearable="isAll" :disabled="disabled" @change="handleChange" style="width:100%">
    <el-option-group v-for="group in alloptions" :key="group.label" :label="_$Z[group.label]">
      <el-option v-for="item in group.options" :key="item.bm"
                 :label="item.mc"
                 :value="item.bm"
                 :disabled="item.disabled">
      </el-option>
    </el-option-group>
  </el-select>
</template>
<script>
import { query } from '@/api/common'
export default {
  name: 'GroupCombox',
  data() {
    return {
      defaultValue: '',
      alloptions: [],
      i18Keys: []
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: function() {
        return false
      }
    },
    isAll: {
      type: Boolean,
      default: function() {
        return true
      }
    },
    value: {
      type: String
    },
    url: {
      type: String
    },
    queryparam: {
      type: Object,
      required: false,
      default: function() {
        return {}
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    handleChange(val) {
      this.$emit('input', val)
    },
    init() {
      query(this.url, this.queryparam).then((result) => {
        const data = result.data.data
        if (data) {
          this.alloptions = data
          if (!this.isAll) {
            this.defaultValue = this.value || this.alloptions[0].options[0].bm
            this.$emit('input', this.defaultValue)
          }
          data.forEach(element => {
            this.i18Keys.push(element.label)
          })
          if (this.value) {
            this.defaultValue = this.value
          }
        }
      })
    }
  },
  watch: {
    value() {
      this.defaultValue = this.value
    },
    queryparam: {
      handler: function(newvalue) {
        this.init()
      },
      deep: true
    }
  }
}
</script>
